//
//  SpotSelectTimerView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/7/2.
//

import UIKit

class SpotSelectTimerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
    typealias SpotSelectTimerViewBlock = () -> Void
    var spotSelectTimerViewBlock : SpotSelectTimerViewBlock!
    var orderInfoModel:OrderListModel!
    var pickerAry : Array<String>!
    var selectBtn:UIButton!
    var appointTimerStr:String!
    var halfTimerStr:String!

    override init(frame: CGRect) {
        super.init(frame: frame)
        pickerAry = [ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_am),
                    ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_pm)]
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
        self.setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI() {
        self.addSubview(self.mainView)
        self.mainView.addSubview(self.cencelBtn)
        self.mainView.addSubview(self.titleLab)
        self.mainView.addSubview(self.contactView)
        self.mainView.addSubview(self.confirmBtn)
        self.mainView.addSubview(self.visitBtn)
        self.mainView.addSubview(self.stationBtn)
        self.mainView.addSubview(self.airportBtn)
        self.mainView.addSubview(self.timerLab)

        self.mainView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(0);
            make.right.equalTo(self.snp.right).offset(0);
            make.bottom.equalTo(self.snp.bottom).offset(0);
            make.height.equalTo(228 + Constants.xSafeBottomHeight);
        }
        self.cencelBtn.snp.makeConstraints { (make) in
            make.height.equalTo(40);
            make.width.equalTo(40);
            make.top.equalTo(self.mainView.snp.top).offset(15);
            make.right.equalTo(self.snp.right).offset(-15);
        }
        self.titleLab.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(15);
            make.top.equalTo(self.mainView.snp.top).offset(15);
        }
        self.confirmBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(60);
            make.right.equalTo(self.mainView.snp.right).offset(-60);
            make.bottom.equalTo(self.mainView.snp.bottom).offset(-(Constants.xSafeBottomHeight == 0 ? 8 : Constants.xSafeBottomHeight));
            make.height.equalTo(40);
        }
        let str = UserDefaults.standard.string(forKey:LanguageHeader.MySystemLanguage)
        var BtnHeightValue:CGFloat = 40
        if (str == LanguageHeader.BurmaLanguage) {
            BtnHeightValue = 60;
        }
        self.visitBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLab.snp.bottom).offset(30);
            make.left.equalTo(self.mainView.snp.left).offset(20);
            make.height.equalTo(BtnHeightValue);
            make.width.equalTo((Constants.screenWidth - 80)/3);
        }
        self.stationBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLab.snp.bottom).offset(30);
            make.centerX.equalTo(self.mainView.snp.centerX);
            make.width.equalTo((Constants.screenWidth - 80)/3);
            make.height.equalTo(self.visitBtn.snp.height);
        }
        self.airportBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.titleLab.snp.bottom).offset(30);
            make.right.equalTo(self.mainView.snp.right).offset(-20);
            make.width.equalTo((Constants.screenWidth - 80)/3);
            make.height.equalTo(self.visitBtn.snp.height);
        }
        self.timerLab.snp.makeConstraints { (make) in
            make.left.equalTo(self.mainView.snp.left).offset(25);
            make.top.equalTo(self.stationBtn.snp.bottom).offset(15);
            make.height.equalTo(28);
        }
        let sizeWidth = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.Contact_service).getWidth(UIFont.systemFont(ofSize: 11))
        self.contactView.snp.makeConstraints { (make) in
            make.left.equalTo(self.timerLab.snp.right).offset(2);
            make.top.equalTo(self.stationBtn.snp.bottom).offset(15);
            make.height.equalTo(28);
            make.width.equalTo(sizeWidth+40);
        }
        self.setvalueUI()
    }

    func setvalueUI() {
        titleLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_expectDeliveryTimer)
        confirmBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_confirm), for: .normal)
        visitBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_timer_standard), for: .normal)
        stationBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_timer_urgent), for: .normal)
        airportBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_timer_assign), for: .normal)
    }
    
    @objc func clickCancelBtn() {
        self.removeFromSuperview()
    }
    
    @objc func clickConfirmBtn()  {
        if selectBtn.tag == 121 {
            orderInfoModel.orderType = 1
        } else if  selectBtn.tag == 122 {
            orderInfoModel.deliveryWay = 2
            orderInfoModel.expectedDeliveryTime = appointTimerStr
            orderInfoModel.expectedDeliveryTimePeriod = halfTimerStr
        } else if  selectBtn.tag == 120 {
            orderInfoModel.orderType = 0
        }
        if (spotSelectTimerViewBlock != nil){
            spotSelectTimerViewBlock()
        }
        self.removeFromSuperview()
    }
    func setOrderInfoModel(orderInfoModel:OrderListModel) {
        self.orderInfoModel = orderInfoModel
        selectBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        selectBtn.backgroundColor = UIColor.white
        selectBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        if orderInfoModel.orderType == 0 {
            selectBtn = visitBtn
            
            timerLab.isHidden = false
            contactView.isHidden = true
            
            if orderInfoModel.standardTimerValue.count > 0 {
                timerLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_predictDeliveryTimer) + orderInfoModel.standardTimerValue.getCurrentTimerByTimezone(dateFormat: "yyyy/MM/dd")
            }
            timerLab.font = UIFont.systemFont(ofSize: 15)
            timerLab.textColor = UIColor.init(hexString: "#000000")
        } else if orderInfoModel.orderType == 1 {
            selectBtn = stationBtn
            
            timerLab.isHidden = false
            contactView.isHidden = false
            timerLab.textColor = UIColor.init(hexString: "#FB4545")
            timerLab.text = "*"+ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.Contact_urgent)
            let text:NSMutableAttributedString = NSMutableAttributedString.init(string: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.Contact_service))
            text.yy_color = UIColor.init(hexString: "#FB4545")
            text.yy_alignment = NSTextAlignment.left
            text.yy_font = UIFont.boldSystemFont(ofSize: 16)
            
            let textsStr:NSString = (text.string) as NSString

            text.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: textsStr.range(of: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.Contact_service)))
            text.yy_setTextHighlight(textsStr.range(of: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.Contact_service)), color: UIColor.init(hexString: "#FB4545"), backgroundColor: UIColor.clear) { (containerView, text, range, rect) in
                self.callPhoneView()
            }
            let attachment = NSMutableAttributedString.yy_attachmentString(withContent: UIImage.init(named: "call_phone"), contentMode: .center, attachmentSize: CGSize(width: 12, height: 12), alignTo: UIFont(name: "PingFangSC-Regular", size: 12)!, alignment: YYTextVerticalAlignment.center)
            text.append(attachment)
            /*
            text.yy_setTextHighlight(textsStr.range(of: String(attachment)), color: UIColor.init(hexString: "#FB4545"), backgroundColor: UIColor.clear) { (containerView, text, range, rect) in
                self.callPhoneView()
            }
 */
            contactView.attributedText = text
            contactView.isHidden = false
        } else {
            selectBtn = airportBtn
            timerLab.isHidden = true
            contactView.isHidden = true
            
            UIView.animate(withDuration: 1.0) {
                self.mainView.snp.updateConstraints { (make) in
                    make.height.equalTo(420 + Constants.xSafeBottomHeight)
                }
                self.mainView.addSubview(self.datePicker)
                self.mainView.addSubview(self.pickerView)
                self.pickerView.snp.makeConstraints { (make) in
                    make.right.equalTo(self.mainView.snp.right).offset(0);
                    make.width.equalTo(Constants.screenWidth/3);
                    make.bottom.equalTo(self.confirmBtn.snp.top).offset(-(27 + Constants.xSafeBottomHeight));
                    make.top.equalTo(self.mainView.snp.top).offset(150);
                }
                self.datePicker.snp.makeConstraints { (make) in
                    make.top.equalTo(self.mainView.snp.top).offset(150);
                    make.left.equalTo(0);
                    make.right.equalTo(self.pickerView.snp.left).offset(0);
                    make.bottom.equalTo(self.confirmBtn.snp.top).offset(-(27 + Constants.xSafeBottomHeight));
                }
            }
        }
        selectBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        selectBtn.layer.borderColor = UIColor.init(hexString: "#FFD10A").cgColor
        selectBtn.backgroundColor = UIColor.init(red: 255/255.0, green: 209/255.0, blue: 10/255.0, alpha: 0.28)
        selectBtn.layer.borderWidth = 0.5
    }
    
    @objc func clickSelectDeliver(sender:UIButton)  {
        if selectBtn == sender {
            return
        }
        selectBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        selectBtn.backgroundColor = UIColor.white
        selectBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        selectBtn = sender
        
        if sender.tag == 120 {
            timerLab.isHidden = false
            contactView.isHidden = false
            
            timerLab.font = UIFont.systemFont(ofSize: 16)
            timerLab.textColor = UIColor.init(hexString: "#000000")
            if orderInfoModel.standardTimerValue.count > 0 {
                timerLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_predictDeliveryTimer) + orderInfoModel.standardTimerValue.getCurrentTimerByTimezone(dateFormat: "yyyy/MM/dd")
                self.datePicker.removeFromSuperview()
                self.pickerView.removeFromSuperview()
                UIView.animate(withDuration: 1.0) {
                    self.mainView.snp.updateConstraints { (make) in
                        make.height.equalTo(228 + Constants.xSafeBottomHeight)
                    }
                }
            }
        } else if sender.tag == 121 {
            timerLab.isHidden = false
            contactView.isHidden = false
            timerLab.text = "*" + ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.Contact_urgent)
            //zs待处理
            
            contactView.isHidden = false
            self.datePicker.removeFromSuperview()
            self.pickerView.removeFromSuperview()
            UIView.animate(withDuration: 1.0) {
                self.mainView.snp.updateConstraints { (make) in
                    make.height.equalTo(228 + Constants.xSafeBottomHeight)
                }
            }
        } else {
            timerLab.isHidden = true
            contactView.isHidden = true
            UIView.animate(withDuration: 1.0) {
                self.mainView.snp.updateConstraints { (make) in
                    make.height.equalTo(420 + Constants.xSafeBottomHeight)
                }
                self.mainView.addSubview(self.datePicker)
                self.mainView.addSubview(self.pickerView)
                self.pickerView.snp.makeConstraints { (make) in
                    make.right.equalTo(self.mainView.snp.right).offset(0);
                    make.width.equalTo(Constants.screenWidth/3);
                    make.bottom.equalTo(self.confirmBtn.snp.top).offset(-(27 + Constants.xSafeBottomHeight));
                    make.top.equalTo(self.mainView.snp.top).offset(150);
                }
                self.datePicker.snp.updateConstraints { (make) in
                    make.top.equalTo(self.mainView.snp.top).offset(150);
                    make.left.equalTo(0);
                    make.right.equalTo(self.pickerView.snp.left).offset(0);
                    make.bottom.equalTo(self.confirmBtn.snp.top).offset(-(27 + Constants.xSafeBottomHeight));
                }
            }
        }
        selectBtn.layer.borderWidth = 0.5
        selectBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        selectBtn.backgroundColor = UIColor.init(red: 255/255.0, green: 209/255.0, blue: 10/255.0, alpha: 0.28)
        selectBtn.layer.borderColor = UIColor.init(hexString: "#FFD10A").cgColor
    }
    
    func callPhoneView() {
        
    }
    
    @objc func datePickerChange(sender:Any) {
        
    }
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerAry.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView.subviews.count > 2 {

        } else {
            
        }
        return pickerAry[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        halfTimerStr = pickerAry[row]
    }
    lazy var mainView: UIView = {
        let mainView = UIView()
        mainView.backgroundColor = UIColor.white
        mainView.layer.masksToBounds = true
        mainView.layer.cornerRadius = 15
        return mainView
    }()
    lazy var cencelBtn: UIButton = {
        let cencelBtn = UIButton.init(type: .custom)
        cencelBtn.setImage(UIImage.init(named: "login_cancel"), for: .normal)
        cencelBtn.addTarget(self, action: #selector(clickCancelBtn), for: .touchUpInside)
        return cencelBtn
    }()
    lazy var titleLab: UILabel = {
        let titleLab = UILabel()
        titleLab.font = UIFont.boldSystemFont(ofSize: 17)
        titleLab.textColor  = UIColor.init(hexString: "#4C4C4C")
        return titleLab
    }()
    lazy var confirmBtn: UIButton = {
        let confirmBtn = UIButton.init(type: .custom)
        confirmBtn.addTarget(self, action: #selector(clickConfirmBtn), for: .touchUpInside)
        confirmBtn.backgroundColor = UIColor.init(hexString: "#FFD10A")
        confirmBtn.layer.masksToBounds = true
        confirmBtn.layer.cornerRadius = 20
        confirmBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        confirmBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        return confirmBtn
    }()
    lazy var visitBtn: UIButton = {
        let visitBtn = UIButton.init(type: .custom)
        visitBtn.addTarget(self, action: #selector(clickSelectDeliver), for: .touchUpInside)
        visitBtn.backgroundColor = UIColor.white
        visitBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        visitBtn.layer.masksToBounds = true
        visitBtn.layer.cornerRadius = 20
        visitBtn.tag = 120
        visitBtn.layer.borderWidth = 0.5
        visitBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        visitBtn.titleLabel?.font = UIFont.systemFont(ofSize: 165)
        return visitBtn
    }()
    lazy var stationBtn: UIButton = {
        let stationBtn = UIButton.init(type: .custom)
        stationBtn.addTarget(self, action: #selector(clickSelectDeliver), for: .touchUpInside)
        stationBtn.backgroundColor = UIColor.white
        stationBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        stationBtn.layer.masksToBounds = true
        stationBtn.layer.cornerRadius = 20
        stationBtn.tag = 121
        stationBtn.layer.borderWidth = 0.5
        stationBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        stationBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        stationBtn.titleLabel?.textAlignment = .center
        stationBtn.titleLabel?.numberOfLines = 0;
        return stationBtn
    }()
    lazy var airportBtn: UIButton = {
        let airportBtn = UIButton.init(type: .custom)
        airportBtn.addTarget(self, action: #selector(clickSelectDeliver), for: .touchUpInside)
        airportBtn.backgroundColor = UIColor.white
        airportBtn.layer.borderColor = UIColor.init(hexString: "#B6B6B6").cgColor
        airportBtn.layer.masksToBounds = true
        airportBtn.layer.cornerRadius = 20
        airportBtn.tag = 122
        airportBtn.layer.borderWidth = 0.5
        airportBtn.setTitleColor(UIColor.init(hexString: "#000000"), for: .normal)
        airportBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        airportBtn.titleLabel?.textAlignment = .center
        airportBtn.titleLabel?.numberOfLines = 0;
        return airportBtn
    }()
    lazy var timerLab: UILabel = {
        let timerLab = UILabel()
        timerLab.font = UIFont.boldSystemFont(ofSize: 16)
        timerLab.textColor  = UIColor.init(hexString: "#000000")
        return timerLab
    }()
    lazy var datePicker: UIDatePicker = {
        let datePicker = UIDatePicker.init()
        datePicker.datePickerMode = UIDatePicker.Mode.date
        datePicker.minimumDate = NSDate.init() as Date
        if #available(iOS 143.4, *) {
            datePicker.preferredDatePickerStyle = UIDatePickerStyle.wheels
        }
        let calendar = Calendar.init(identifier: Calendar.Identifier.gregorian)
        var comps = DateComponents.init()
        comps.day = 14
        let maxDate = calendar.date(byAdding: comps, to: Date.init())
        datePicker.maximumDate = maxDate
        datePicker.addTarget(self, action: #selector(datePickerChange), for: .valueChanged)
        let formatter = DateFormatter.init()
        let str = UserDefaults.standard.string(forKey:LanguageHeader.MySystemLanguage)
        if (str == LanguageHeader.ChinaLanguage) {
            datePicker.locale = Locale.init(identifier: "zh")
            formatter.date(from: "yyyy/MM/dd")
        } else if (str == LanguageHeader.BurmaLanguage) {
            datePicker.locale = Locale.init(identifier: "en")
            formatter.date(from: "yyyy/MM/dd")
        } else {
            datePicker.locale = Locale.init(identifier: "en")
            formatter.date(from: "yyyy/MM/dd")
        }
        let dateString = formatter.string(from: datePicker.date)
        appointTimerStr = dateString
        return datePicker
    }()
    lazy var pickerView: UIPickerView = {
        let pickerView = UIPickerView.init()
        pickerView.backgroundColor = UIColor.clear
        pickerView.delegate = self
        pickerView.dataSource = self
        halfTimerStr = pickerAry[0]
        return pickerView
    }()
    lazy var contactView: YYLabel = {
        let contactView = YYLabel.init()
        contactView.preferredMaxLayoutWidth = Constants.screenWidth - 60
        contactView.numberOfLines = 0
        contactView.textVerticalAlignment = YYTextVerticalAlignment.center
        contactView.backgroundColor = UIColor.white
        return contactView
    }()
    lazy var contactUSView: ContactUSView = {
        let contactUSView = ContactUSView.init(frame: CGRect.init(x: 0, y: 0, width: Constants.screenWidth, height: Constants.screenHeight))
        return contactUSView
    }()
}
